package se.webbzon.boltzmann.menu;

import java.awt.image.ImageObserver;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Mixer.Info;

import se.webbzon.oschi01.sound.ClipSound;
import se.webbzon.oschi01.sound.DataLineSound;
import se.webbzon.oschi01.utilities.StringUtils;

public class AudioPage extends MFlowPage {
	
	// The layout of this audio page
	private final AbstractGameMenuLayout layout;
	
	// The audio device radio group
	private final DeviceGroup group;
	
	/*============================================================
	Constructors
	============================================================*/

	public AudioPage(ImageObserver observer, AbstractGameMenuLayout layout, MPage parent) {
		super(observer, layout.getPageLayout(), parent, "Audio");
		this.layout = layout;
		this.group = new DeviceGroup();
		group.setValue(ClipSound.getDefaultMixer());
		
		addItem(new PageLabel(observer, layout.getLabelLayout(),this));
		addRow(0.25);
		addItem(new ReturnButton(getImageObserver(),layout.getButtonLayout(), parent));
		addRow();
		addItem(new ApplyButton());
		addRow();
		Info[] mixerInfos = AudioSystem.getMixerInfo();
		for (int row = 0; row < 4; row++) {
			for (int col = 0; col < 3; col++) {
				int index = row * 3 + col;
				if (index == 0)
					addItem(new DefaultDeviceRadioButton());
				else if (index <= mixerInfos.length)
					addItem(new DeviceRadioButton(mixerInfos[index-1]));
			}
			addRow(0.25);
		}
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public void onEnter(GameMenu menu) {};

	@Override public void onLeave(GameMenu menu) {};
	
	/*============================================================
	Private Classes
	============================================================*/
	
	/** The apply button of this audio page. **/
	private final class ApplyButton extends MButton {

		public ApplyButton() {
			super(getImageObserver(), layout.getButtonLayout(), false, "Apply");
		}

		@Override public void onClick(GameMenu menu) {
			Object mixer = group.getValue();
			if (mixer == null) {
				ClipSound.setDefaultMixer(null);
				DataLineSound.setDefaultMixer(null);
			} else if (mixer instanceof Info) {
				ClipSound.setDefaultMixer((Info) mixer);
				DataLineSound.setDefaultMixer((Info) mixer);
			}
		}
		
	}
	
	/** Audio device radio group. **/
	private final class DeviceGroup extends MRadioGroup {

		@Override public void onChangeValue(Object value) {};
		
	}
	
	/** Default audio device radio button. **/
	private final class DefaultDeviceRadioButton extends MRadio {

		public DefaultDeviceRadioButton() {
			super(AudioPage.this.getImageObserver(), layout.getButtonLayout(), group, null, false, "Auto (default)");
		}
		
	}
	
	/** Audio device radio button. **/
	private final class DeviceRadioButton extends MRadio {

		public DeviceRadioButton(Info mixerInfo) {
			super(AudioPage.this.getImageObserver(), layout.getButtonLayout(), group, mixerInfo, false, StringUtils.divide(mixerInfo.getName(), 17));
		}
		
	}

}
